﻿using System;
using System.Web.Http;
using System.Collections.Specialized;
using PreeRegisterModel;
using PreeRegisterBLL;
using PreeRegisterBLL.Security;

namespace PreeRegister_WebAPI.Controllers
{
    public class orderController : ApiController
    {
        [HttpPost]
        /// <summary>
        /// 预约挂号【调用医院模式】
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel getorderold(NameValueCollection any)
        {
            try
            {
                //获取JSON加密参数
                string head_json = CommonBLL.RequestGetParam("head").ToString();
                string body_json = CommonBLL.RequestGetParam("body").ToString();

                //拼装jsonData类
                EncodeModel encodeModel = new EncodeModel();
                encodeModel.head = head_json;
                encodeModel.body = body_json;

                //转化jsonData串
                string jsonData = csSerialize.ResponseInfo(encodeModel);

                //解密头和内容信息
                string ErrMsg = "";
                string ReturnBodyJson = "";
                if (!ResultDecode.DecodeResult(jsonData, "", ref head_json, ref body_json, ref ErrMsg))
                {
                    OrderResponseReturnModel orderResponseReturnModel = new OrderResponseReturnModel();
                    orderResponseReturnModel.code = "0";
                    orderResponseReturnModel.msg = "预约失败:" + ErrMsg;

                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.OrderResponseBodyInfo(orderResponseReturnModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("Order|getorder|Post|\r\n" + body_json + "\r\n\r\n");

                //反序列化请求字符串
                OrderRequestModel orderRequestModel = OrderBLL.ReturnOrderRequestModel(head_json, body_json);

                //声明请求类_主索引标志
                OrderParameterModel_Index orderParameterModel_Index = new OrderParameterModel_Index();
                orderParameterModel_Index.addr = orderRequestModel.body.addr;
                orderParameterModel_Index.atype = orderRequestModel.body.atype;
                orderParameterModel_Index.birth = orderRequestModel.body.birth;
                orderParameterModel_Index.card = orderRequestModel.body.card;
                orderParameterModel_Index.cid = orderRequestModel.body.cid;
                orderParameterModel_Index.ctype = orderRequestModel.body.ctype;
                orderParameterModel_Index.gender = orderRequestModel.body.gender;
                orderParameterModel_Index.iid = orderRequestModel.body.iid;
                orderParameterModel_Index.indexType = "0";
                orderParameterModel_Index.inum = orderRequestModel.body.inum;
                orderParameterModel_Index.name = orderRequestModel.body.name;
                orderParameterModel_Index.oid = orderRequestModel.body.oid;
                orderParameterModel_Index.ptype = orderRequestModel.body.ptype;
                orderParameterModel_Index.rtype = orderRequestModel.body.rtype;
                orderParameterModel_Index.source = CommonBLL.GetConfig("source") == "1" ? "趣医网" : "健康山西";
                orderParameterModel_Index.tel = orderRequestModel.body.tel;
                orderParameterModel_Index.time = orderRequestModel.body.time;
                orderParameterModel_Index.wid = orderRequestModel.body.wid;
                orderParameterModel_Index.docid = orderRequestModel.body.docid;
                orderParameterModel_Index.preeflag = "1";

                //执行预约
                OrderResponseModel orderResponseModel = OrderBLL.Order(orderParameterModel_Index);

                //序列化返回内容类
                ReturnBodyJson = csSerialize.OrderResponseBodyInfo(orderResponseModel.body);

                //返回参数日志
                CommonBLL.WriteLog("Order|getorder|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
            catch (Exception err)
            {
                OrderResponseReturnModel orderResponseReturnModel = new OrderResponseReturnModel();
                orderResponseReturnModel.code = "0";
                orderResponseReturnModel.msg = "预约失败:" + err.Message;

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.OrderResponseBodyInfo(orderResponseReturnModel);

                //返回参数日志
                CommonBLL.WriteLog("Order|getorder|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }

        [HttpPost]
        /// <summary>
        /// 预约挂号【模拟调用模式】
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel getorder(NameValueCollection any)
        {
            try
            {
                //获取JSON加密参数
                string head_json = CommonBLL.RequestGetParam("head").ToString();
                string body_json = CommonBLL.RequestGetParam("body").ToString();
               
                //拼装jsonData类
                EncodeModel encodeModel = new EncodeModel();
                encodeModel.head = head_json;
                encodeModel.body = body_json;

                //转化jsonData串
                string jsonData = csSerialize.ResponseInfo(encodeModel);
                OrderResponseModel orderResponseModel = new OrderResponseModel();
                //解密头和内容信息
                string ErrMsg = "";
                string ReturnBodyJson = "";
                if (!ResultDecode.DecodeResult(jsonData, "", ref head_json, ref body_json, ref ErrMsg))
                {
                    OrderResponseReturnModel orderResponseReturnModel = new OrderResponseReturnModel();
                    orderResponseReturnModel.code = "0";
                    orderResponseReturnModel.msg = "预约失败:" + ErrMsg;

                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.OrderResponseBodyInfo(orderResponseReturnModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteFile_New(CommonBLL.bizCode_getorder, "1：预约挂号请求参数：\r\n" + CommonBLL.ConvertJsonString(body_json.ToString()));

                //反序列化请求字符串
                OrderRequestModel orderRequestModel = OrderBLL.ReturnOrderRequestModel(head_json, body_json);

                //声明请求类_主索引标志
                OrderParameterModel_Index orderParameterModel_Index = new OrderParameterModel_Index();
                orderParameterModel_Index.addr = orderRequestModel.body.addr;
                orderParameterModel_Index.atype = orderRequestModel.body.atype;
                orderParameterModel_Index.birth = orderRequestModel.body.birth;
                orderParameterModel_Index.card = orderRequestModel.body.card;
                orderParameterModel_Index.cid = orderRequestModel.body.cid;
                orderParameterModel_Index.ctype = orderRequestModel.body.ctype;
                orderParameterModel_Index.gender = orderRequestModel.body.gender;
                orderParameterModel_Index.iid = orderRequestModel.body.iid;
                orderParameterModel_Index.indexType = "0";
                orderParameterModel_Index.inum = orderRequestModel.body.inum;
                orderParameterModel_Index.name = orderRequestModel.body.name;
                orderParameterModel_Index.oid = orderRequestModel.body.oid;
                orderParameterModel_Index.ptype = orderRequestModel.body.ptype;
                orderParameterModel_Index.rtype = orderRequestModel.body.rtype;
                orderParameterModel_Index.source = CommonBLL.GetConfig("source") == "1" ? "趣医网" : "健康山西";
                orderParameterModel_Index.tel = orderRequestModel.body.tel;
                orderParameterModel_Index.time = orderRequestModel.body.time;
                orderParameterModel_Index.wid = orderRequestModel.body.wid;
                orderParameterModel_Index.docid = orderRequestModel.body.docid;
                orderParameterModel_Index.preeflag = "1";

                //执行预约
                orderResponseModel = OrderBLL.Order(orderParameterModel_Index);

                //序列化返回内容类
                ReturnBodyJson = csSerialize.OrderResponseBodyInfo(orderResponseModel.body);

                //返回参数日志
                CommonBLL.WriteFile_New(CommonBLL.bizCode_getorder, "3：预约挂号返回参数：\r\n"
                    + CommonBLL.ConvertJsonString(ReturnBodyJson.ToString()));


                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
            catch (Exception err)
            {
                OrderResponseReturnModel orderResponseReturnModel = new OrderResponseReturnModel();
                orderResponseReturnModel.code = "0";
                orderResponseReturnModel.msg = "预约失败:" + err.Message;

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.OrderResponseBodyInfo(orderResponseReturnModel);

                //返回参数日志
                CommonBLL.WriteFile_New(CommonBLL.bizCode_getorder, "3：预约挂号返回参数：\r\n"
                    + CommonBLL.ConvertJsonString(ReturnBodyJson.ToString()));

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }

        [HttpPost]
        /// <summary>
        /// 取消预约
        /// http://172.30.0.131:10018/api/order/cancelorder
        /// http://192.168.1.131:8080/api/order/cancelorder
        /// jsonData={"head":{"fromtype":"UH_RDSP_ISO","sessionid":"10","time":"1432603753540","token":"f6f0020aadc680a62a0550e8800482ae","version":"1"},"body":{"atype":"1","inum":4,"oid":"1505280001","iid":5374407,"tel":"13546303705","name":"张三","ctype":null,"cid":null,"wid":"213642"}}
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel cancelorder(NameValueCollection any)
        {
            try
            {
                //获取JSON加密参数
                string jsonData = CommonBLL.RequestGetParam("jsonData").ToString();
                string head_json = "";
                string body_json = "";

                //校验jsonData值
                if (jsonData.Trim() == "")
                {
                    //获取JSON加密参数
                    head_json = CommonBLL.RequestGetParam("head").ToString();
                    body_json = CommonBLL.RequestGetParam("body").ToString();

                    //拼装jsonData类
                    EncodeModel encodeModel = new EncodeModel();
                    encodeModel.head = head_json;
                    encodeModel.body = body_json;

                    //转化jsonData串
                    jsonData = csSerialize.ResponseInfo(encodeModel);
                }

                string ReturnBodyJson = "";
                CancelOrderResponseModel cancelOrderResponseModel = new CancelOrderResponseModel();

                //解密头和内容信息
                string ErrMsg = "";

                if (!ResultDecode.DecodeResult(jsonData, "order|cancelorder", ref head_json, ref body_json, ref ErrMsg))
                {

                    cancelOrderResponseModel.code = "0";
                    cancelOrderResponseModel.msg = "取消预约失败:" + ErrMsg;

                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.CancelOrderResponseInfo(cancelOrderResponseModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteFile_New(CommonBLL.bizCode_cancelorder, "1：取消预约请求参数：\r\n" + CommonBLL.ConvertJsonString(body_json.ToString()));

                //反序列化请求字符串
                CancelOrderRequestModel cancelOrderRequestModel = new CancelOrderRequestModel();
                CancelOrderRequestModel_lk cancelOrderRequestModel_lk = new CancelOrderRequestModel_lk();
                //非联康版  liujia160102
                if (body_json.Contains("orderid"))
                {
                    cancelOrderRequestModel = csSerialize.CancelOrderRequestInfo(body_json);
                }
                else  //联康版
                {
                    cancelOrderRequestModel_lk = csSerialize.CancelOrderRequestModel_lk(body_json);
                    cancelOrderRequestModel.atype = cancelOrderRequestModel_lk.atype;
                    cancelOrderRequestModel.iid = cancelOrderRequestModel_lk.iid;
                    cancelOrderRequestModel.inum = cancelOrderRequestModel_lk.inum;
                    cancelOrderRequestModel.oid = cancelOrderRequestModel_lk.oid;
                    cancelOrderRequestModel.wid = cancelOrderRequestModel_lk.wid;
                    cancelOrderRequestModel.cid = cancelOrderRequestModel_lk.cid;
                    cancelOrderRequestModel.ctype = cancelOrderRequestModel_lk.ctype;
                    cancelOrderRequestModel.name = cancelOrderRequestModel_lk.name;
                    cancelOrderRequestModel.tel = cancelOrderRequestModel_lk.tel;
                    cancelOrderRequestModel.orderid = "";
                    cancelOrderRequestModel.source = CommonBLL.GetConfig("source") == "1" ? "趣医网" : "健康山西";
                }
                // OrderInfoModel orderInfo = OrderExeInfoBLL.GetParentInfo(cancelOrderRequestModel.oid);

                //执行取消预约 标记中间表状态为已取消
                cancelOrderResponseModel = CancelOrderBLL.CancelOrder_New(cancelOrderRequestModel);

                //序列化返回内容类
                ReturnBodyJson = csSerialize.CancelOrderResponseInfo(cancelOrderResponseModel);

                //返回参数日志
                CommonBLL.WriteFile_New(CommonBLL.bizCode_cancelorder, "2：取消预约返回参数：\r\n" + CommonBLL.ConvertJsonString(ReturnBodyJson.ToString()));

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
            catch (Exception err)
            {
                CancelOrderResponseModel cancelOrderResponseModel = new CancelOrderResponseModel();
                cancelOrderResponseModel.code = "0";
                cancelOrderResponseModel.msg = "预约失败:" + err.Message;

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.CancelOrderResponseInfo(cancelOrderResponseModel);

                //返回参数日志
                CommonBLL.WriteFile_New(CommonBLL.bizCode_getorder, "3：取消预约返回参数：\r\n" + CommonBLL.ConvertJsonString(ReturnBodyJson.ToString()));

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }

        [HttpPost]
        /// <summary>
        /// 预约执行情况更新
        /// http://172.30.0.131:10018/api/order/update
        /// http://192.168.0.103:8080/api/order/update
        /// jsonData={"head":{"fromtype":"UH_RDSP_ISO","sessionid":"10","time":"1432603753540","token":"f6f0020aadc680a62a0550e8800482ae","version":"1"},"body":{"optype":"2","hospid":"66","ids":"376390,465947"}}
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel update(NameValueCollection any)
        {
            try
            {

                //获取JSON加密参数
                string head_json = CommonBLL.RequestGetParam("head").ToString();
                string body_json = CommonBLL.RequestGetParam("body").ToString();

                //拼装jsonData类
                EncodeModel encodeModel = new EncodeModel();
                encodeModel.head = head_json;
                encodeModel.body = body_json;

                //转化jsonData串
                string jsonData = csSerialize.ResponseInfo(encodeModel);

                //解密头和内容信息
                string ErrMsg = "";
                string ReturnBodyJson = "";
                if (!ResultDecode.DecodeResult(jsonData, "", ref head_json, ref body_json, ref ErrMsg))
                {
                    OrderExeInfoBodyModel orderExeInfoBodyModel = new OrderExeInfoBodyModel();

                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.OrderExeResponseBodyInfo(orderExeInfoBodyModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("Order|update|Post|\r\n" + body_json + "\r\n\r\n");

                //反序列化请求字符串
                OrderExeInfoRequestModel orderExeInfoRequestModel = OrderExeInfoBLL.ReturnOrderExeRequestModel(head_json, body_json);

                //查询预约执行情况
                OrderExeInfoResponseModel orderExeInfoResponseModel = OrderExeInfoBLL.OrderUpdate(orderExeInfoRequestModel);

                //序列化返回内容类
                ReturnBodyJson = csSerialize.OrderExeResponseBodyInfo(orderExeInfoResponseModel.body);

                //返回参数日志
                CommonBLL.WriteLog("Order|update|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                //string result = ResultDecode.EncodeResult(ReturnBodyJson);

                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
            catch (Exception err)
            {
                OrderExeInfoBodyModel orderExeInfoBodyModel = new OrderExeInfoBodyModel();

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.OrderExeResponseBodyInfo(orderExeInfoBodyModel);

                //返回参数日志
                CommonBLL.WriteLog("Order|update|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }
    }
}
